<?php
/**
 * File : TypeDocumentDAO.php
 * Created by Ludovic Reenaers
 * Created on  24 nov. 2010
 * lreenaers@hotmail.com
 */
class TypeDocumentDAO extends DAO{
	public function getById($id){
		try
		{
			$id =Validator::escapeSql($id);
			$query="select * from TYPE_DOCUMENT where id=$id;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$entiteDAO = $this->factory->getEntiteDAO();
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new TypeDocument((int)$Bean->ID);
				$thebean->setLabel($Bean->LABEL);
				$thebean->setAvailable($Bean->AVAILABLE);
				$thebean->setEntite($entiteDAO->getById((int)$Bean->ENTITE_ID));
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;
	}
	function remove($Bean){
		try{
				
			$query="update TYPE_DOCUMENTS set AVAILABLE= false where id=".Validator::escapeSql($Bean->getId())."';";
			$this->pdo->exec($query);
			$bool=true;
		}catch(Exception $e)    {
			$bool=false;
		}
		return $bool;

	}
	function update($Bean){
		try{
			$entiteDAO = $this->factory->getEntiteDAO();
			$entiteDAO->update($Bean->getEntite());
			$query = "update TYPE_DOCUMENT set LABEL='".Validator::escapeSql($Bean->getLabel())."',ENTITE_ID =".Validator::escapeSql($Bean->getEntite()->getId())." ,AVAILABLE=".Validator::escapeSql($Bean->getAvailable()).";";
			$this->pdo->exec($query);
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;

		}
		return $bool;
	}
	function add($Bean){
		try{
			if (is_null($Bean->getId())){
				$thebean = null;
				$insertQuery = "insert into TYPE_DOCUMENT (LABEL,AVAILABLE,ENTITE_ID) values ('".Validator::escapeSql($Bean->getLabel())."',".Validator::escapeSql($Bean->getAvailable()).",".Validator::escapeSql($Bean->getEntite()->getId()).");";
				$entiteDAO = $this->factory->getEntiteDAO();
				$entiteDAO->add($Bean->getEntite());
				$this->pdo->exec($insertQuery);
				$thebean = $this->getById((int)$this->pdo->lastInsertId());
			}
		}catch(Exception $e)    {
			$this->pdo->message;
		}
		return $thebean;
	}

}
?>